using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Core;

namespace Yuanlin.Business
{
    public class AquacultureSection_SectionList : YuanlinUserControl,
        IReplyConverter<AquacultureSection_SectionReply, GetListOperation>
    {
        protected Repeater list;
        protected HeadControl headControl;
        private EntityList sections;
        public int aquacultureId = UrlManager.Instance.GetParameterValue<int>(YuanlinParameterName.AquacultureId);
        public int categoryType = UrlManager.Instance.GetParameterValue<int>(YuanlinParameterName.CategoryType);
        public int categoryId = UrlManager.Instance.GetParameterValue<int>(YuanlinParameterName.CategoryId);

        public override void GetRequests(List<BaseRequestBinder> requestBinders)
        {
            AquacultureSection_SectionListRequest request = new AquacultureSection_SectionListRequest();
            request.AquacultureId = aquacultureId;
            request.CategoryType = categoryType;
            request.CategoryId = categoryId;
            requestBinders.Add(RequestHelper.BuildGetAquacultureSection_SectionListRequest(this, request));
        }
        protected override void OnLoad(EventArgs e)
        {
            AjaxManager.Register(this, "AjaxMethod");
        }

        private void BindData()
        {
            if (list != null)
            {
                list.DataSource = sections;
                list.DataBind();
            }
            headControl.TitleResourceName = "ProductSection" + categoryId;
        }

        #region Ajax Method

        [AjaxMethod(IncludeControlValuesWithCallBack = false)]
        public void DeleteAquacultureSection_Section(int sectionId)
        {
            BusinessManager.DeleteAquacultureSection_Section(sectionId);
        }

        [AjaxMethod(IncludeControlValuesWithCallBack = false)]
        public void DeleteAquacultureSection_Sections(string sectionIds)
        {
            if (string.IsNullOrEmpty(sectionIds))
            {
                return;
            }
            string[] sectionIdArray = sectionIds.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
            int sectionId = 0;
            foreach (string stringSectionId in sectionIdArray)
            {
                try
                {
                    sectionId = Int32.Parse(stringSectionId);
                }
                catch
                {
                    continue;
                }
                if (sectionId > 0)
                {
                    DeleteAquacultureSection_Section(sectionId);
                }
            }
        }

        #endregion

        #region IReplyConverter<AquacultureSection_SectionReply,GetListOperation> Members

        public void ConvertReply(AquacultureSection_SectionReply reply, GetListOperation operation)
        {
            if (reply != null)
            {
                sections = reply.EntityList;

                if (!Page.IsPostBack && !AjaxManager.IsCallBack)
                {
                    BindData();
                }
            }
        }

        #endregion
    }
}